#
# Build system for the EGS system.  Make sure to edit config.mk to your liking.
#
# @author Jonathan Walsh (jwalsh8484@gmail.com)
#

include config.mk

#Some build constants
JAVAC=javac -cp $(CLASS_DIR)
AT="\@"

# Lists of source files
PACKET_SRC=Packet.java UseSecurePacket.java UseSecureResponsePacket.java AuthLoginPacket.java AuthLoginErrPacket.java GameListPacket.java SelectGamePacket.java InvalidGamePacket.java OppNackPacket.java OppListPacket.java OppChoicePacket.java OppOfferPacket.java OppNackPacket.java OppAckPacket.java BoardSendPacket.java MoveSendPacket.java MoveResponsePacket.java EndGamePacket.java DisconnectPacket.java
PACKET_SRC+=TestPackets.java PacketException.java InvalidPacketException.java

GAME_SRC=Game.java GameException.java
GAME_SRC+=CheckersGame.java CheckersClient.java Move.java SquareState.java
GAME_SRC+=TicTacToeGame.java TicTacToeGameException.java

COMMS_SRC=Connection.java ServerConnection.java ClientConnection.java EGSCommunicationException.java StubConnection.java

CLIENT_SRC=EGSClient.java ClientInput.java

SERVER_SRC=EGSServer.java ClientThread.java ClientState.java ClientManager.java GameManager.java AuthenticationManager.java


# Main target, hits all the modules
all: CLASS_DIR packets games comms server client

CLASS_DIR:
	mkdir -p $(CLASS_DIR)

clean:
	rm -rf $(CLASS_DIR)
	rm -f test_packets.sh run_client.sh run_server.sh




### Indivdual submodules below here ###

# GAME BUILD PATH
GAME_DIR=$(SRC_DIR)/games
GAME_SRC_FULL=$(addprefix $(SRC_DIR)/games/,$(GAME_SRC))
games: $(GAME_SRC_FULL)
	$(JAVAC) $(GAME_SRC_FULL) -d $(CLASS_DIR)


# COMMS BUILD PATH
COMMS_DIR=$(SRC_DIR)/comms
COMMS_SRC_FULL=$(addprefix $(SRC_DIR)/comms/,$(COMMS_SRC))
comms: $(COMMS_SRC_FULL)
	$(JAVAC) -cp $(CLASS_DIR) $(COMMS_SRC_FULL) -d $(CLASS_DIR)


# CLIENT BUILD PATH
CLIENT_DIR=$(SRC_DIR)/client
CLIENT_SRC_FULL=$(addprefix $(SRC_DIR)/client/,$(CLIENT_SRC))
client: $(CLIENT_SRC_FULL) run_client.sh
	$(JAVAC) $(CLIENT_SRC_FULL) -d $(CLASS_DIR)
run_client.sh:
	echo -e "#!/bin/bash\njava -Djavax.net.ssl.keyStore=checkersKeyStore.jks -Djavax.net.ssl.keyStorePassword=checkers -Djavax.net.ssl.trustStore=checkersKeyStore.jks -Djava.net.ssl.trustStorePassword=checkers -cp $(CLASS_DIR) egs.client.EGSClient $$$(AT)" > run_client.sh
	chmod +x run_client.sh


# SERVER BUILD PATH
SERVER_DIR=$(SRC_DIR)/server
SERVER_SRC_FULL=$(addprefix $(SRC_DIR)/server/,$(SERVER_SRC))
server: $(SERVER_SRC_FULL) run_server.sh
	$(JAVAC) $(SERVER_SRC_FULL) -d $(CLASS_DIR)
run_server.sh:
	echo -e "#!/bin/bash\njava -Djavax.net.ssl.keyStore=checkersKeyStore.jks -Djavax.net.ssl.keyStorePassword=checkers -Djavax.net.ssl.trustStore=checkersKeyStore.jks -Djava.net.ssl.trustStorePassword=checkers -cp $(CLASS_DIR) egs.server.EGSServer $$$(AT)" > run_server.sh
	chmod +x run_server.sh


# PACKET BUILD PATH
PACKET_DIR=$(SRC_DIR)/packets
PACKET_SRC_FULL=$(addprefix $(SRC_DIR)/packets/,$(PACKET_SRC))
packets: $(PACKET_SRC_FULL) test_packets.sh
	$(JAVAC) $(PACKET_SRC_FULL) -d $(CLASS_DIR)
test_packets.sh:
	echo -e "#!/bin/bash\njava -cp $(CLASS_DIR) egs.packets.TestPackets" > test_packets.sh
	chmod +x test_packets.sh
